<?php


class portal extends Controller {

	protected $user;

	function __construct() {
		parent::__construct();
		$this->session();
		$this->user = new user();
	}
	
	private function secure() {
		if( $_SESSION['portaladmin'] !== true ) {
			header('Location: /portal');
		}
	}

	function index() {
		$user = '';
		$pass = '';
		if( $_SERVER['REQUEST_METHOD'] == 'POST') {
			if( $_POST['username'] == 'admin' 
			&& $_POST['password'] == 'admin' ) {
				$_SESSION['portaladmin'] = true;
				header('Location: /portal/users'); 
			}
		}
		new View('portal/template', array(
			'content' => BufferedView::instance('portal/login', array(
				'user' => $user,
				'pass' => $pass,
				'action' => '/portal'
			) ),
		) );
	}
	
	function login() {
	
	}
	
	function users() {
		$users = $this->user->getAllUsers();
		new View('template', array( 
			'heading' => 'Manage Users',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
			'content' => BufferedView::instance('portal/users', array(
				'users' => $users
			) )->getBuffer()
			. BufferedView::instance('portal/form', array(
				'action' => '/portal/add'
			) )->getBuffer()
		) );
	}
	
	function add($param = NULL) {
		$this->user->insertUserComplete(
			$_POST['stud_no'], 
			$_POST['password'], 
			$_POST['lastname'], 
			$_POST['firstname'], 
			$_POST['middlename'], 
			$_POST['admin'], 
			$_POST['email']
		);
		header('Location: /portal/users');
		new View('template', array( 
			'heading' => 'Manage Users',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
			'content' => BufferedView::instance('portal/form', array(
				'users' => $users
			) )
		) );
	}
	
	function remove($param = NULL) {
		$this->user->deleteUser($param);
		header('Location: /portal/users');
		new View('template', array( 
			'heading' => 'Manage Users',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
			'content' => BufferedView::instance('portal/form', array(
				'users' => $users
			) )
		) );
	}
	
	function update($param = NULL) {
		$row = $this->user->findUser($param);
		$row = $row[0];
		new View('template', array( 
			'heading' => 'Manage Users',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
			'content' => BufferedView::instance('portal/form', array(
				'action' => '/portal/update_confirm',
				'studno' => $row['stud_no'],
				'password' => $row['password'],
				'lastname' => $row['lastname'],
				'firstname' => $row['firstname'],
				'middlename' => $row['middlename'],
				'admin' => $row['admin'],
				'email' => $row['email']
			) )
		) );
	}
	
	function update_confirm() {
		$this->user->updateUserComplete(
			$_POST['stud_no'], 
			$_POST['lastname'], 
			$_POST['firstname'], 
			$_POST['middlename'], 
			$_POST['password'], 
			$_POST['admin'], 
			$_POST['email']
		);
		header('Location: /portal/users');
	}

}



























	
